package com.sba.core.service.impl;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.sba.core.domain.Menu;
import com.sba.core.domain.Organization;
import com.sba.core.domain.Role;
import com.sba.core.domain.User;
import com.sba.core.repository.OrganizationRepository;
import com.sba.core.repository.RoleRepository;
import com.sba.core.repository.UserRepository;
import com.sba.core.service.UserService;

/**
 * 
 * @author feiliming
 *
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService{

	//private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Autowired
	UserRepository userRepository;
	
	@Autowired
	RoleRepository roleRepository;
	
	@Autowired
	OrganizationRepository organizationRepository;
	
	@Override
	public User getById(Long id){
		User user = userRepository.findOne(id);
		if(user.getOrganizationId() != null && user.getOrganizationId() != 0){
			Organization organization = organizationRepository.findOne(user.getOrganizationId());
			user.setOrganizationName(organization.getName());
		}else{
			user.setOrganizationName("");
		}
		return user;
	}

	@Override
	public List<User> getByLoginName(String loginName) {
		return userRepository.findByLoginNameAndIsDelete(loginName, 0);
	}

	@Override
	public List<User> getByLoginNameAndIsDisabled(String loginName, int isDisabled) {
		return userRepository.findByLoginNameAndIsDisabledAndIsDelete(loginName, isDisabled, 0);
	}

	@Override
	public List<User> getByLoginNameAndPassword(String loginName, String password) {
		return userRepository.findByLoginNameAndPasswordAndIsDelete(loginName, password, 0);
	}

	@Override
	public Page<User> getUsers(int limit, int offset) {
		return userRepository.findByIsDelete(0, buildPageRequest(limit, offset));
	}

	@Override
	public Page<User> getUsers(Long organizationId, int limit, int offset) {
		return userRepository.findByOrganizationIdAndIsDelete(organizationId, 0, buildPageRequest(limit, offset));
	}
	
	private PageRequest buildPageRequest(int limit, int offset){
		Order typeOrder = new Order(Direction.ASC, "loginName");
		Order sortOrder = new Order(Direction.ASC, "createTime");
		Sort sort = new Sort(typeOrder, sortOrder);
		int pageNumber = (offset + limit)/limit;
		int pageSize = limit;
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sort);
		return pageRequest;
	}

	@Override
	public Set<Menu> getMenusByUserId(Long userId) {
		User user = userRepository.findOne(userId);
		
		Set<Menu> menus = new TreeSet<Menu>();
		for(Role role : user.getRoles()){
			for(Menu menu : role.getMenus()){
				if(menu.getIsShow() == 1)
					menus.add(menu);
			}
		}
		return menus;
	}
	
	/**@Override
	public void delete(String ids) {
		String[] sids = ids.split(",");
		Set<User> users = new HashSet<User>();
		for(String sid : sids){
			if(StringUtil.isBlank(sid)){
				continue;
			}
			User user = new User();
			user.setId(Long.parseLong(sid));
			users.add(user);
		}
		userRepository.delete(users);
	}*/
	
}
